﻿/* 
 * Since 2008 - 2012
 */

package com.hh.bss.auth.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import com.hh.bss.auth.dao.*;
import com.hh.bss.auth.entity.*;
import com.hh.bss.auth.service.*;
import com.hh.bss.auth.vo.query.*;
import com.hh.bss.base.*;
import com.hh.bss.common.cache.SystemModuleCache;
import com.hh.bss.common.page.*;
import com.hh.bss.common.page.impl.*;
import com.hh.bss.project.entity.ContractJob;
import com.hh.bss.util.*;




@Service
@Transactional
public class SystemResourceServiceImpl extends BaseBusinessService<SystemResource,java.lang.Integer>  implements SystemResourceService<SystemResource,java.lang.Integer>{

	private SystemResourceDao systemResourceDao;
	/**增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写*/
	public void setSystemResourceDaoImpl(SystemResourceDao dao) {
		this.systemResourceDao = dao;
	}
	public EntityDao getEntityDao() {
		return this.systemResourceDao;
	}
	
	@Transactional(readOnly=true)
	public Page findPage(SystemResourceQuery query) {
		return systemResourceDao.findPage(query);
	}
	
	@Override
	public Page findResourcePage(SystemResourceQuery query) {
		// TODO Auto-generated method stub
		//a.id,a.name,a.module_id moduleId,b.roleIds,rootModuleName,moduleName
		Page page=systemResourceDao.findResourcePage(query);
		List systemResourceList = (List) page.getResult();
		
		Map m=null;
		for(int i=0;systemResourceList!=null&&i<systemResourceList.size();i++){
			m=(Map)systemResourceList.get(i);			
			m.put("rootModuleName", SystemModuleCache.getRootModuleName((Integer)m.get("moduleId")));
			m.put("moduleName", SystemModuleCache.getModuleName((Integer)m.get("moduleId")));
		}
		return page;
		
	}
	
	private boolean isEqualsSet(String s1,String s2){
		//
		if (StringHelper.isEmpty(s1)&&StringHelper.isEmpty(s2)) return true;
		if (StringHelper.isEmpty(s1)&&!StringHelper.isEmpty(s2)) return false;
		if (!StringHelper.isEmpty(s1)&&StringHelper.isEmpty(s2)) return false;
		String[] ss1=s1.split(",");
		String[] ss2=s2.split(",");
		if (ss1.length!=ss2.length) return false;
		for(int i=0;i<ss1.length;i++){
			boolean isEqual=false;
			String ts1=ss1[i];
			for(int j=0;j<ss2.length;j++){
				String ts2=ss2[i];
				if (ts1.equals(ts2)) isEqual=true; 
			}
			if(!isEqual) return false;
		}
		return true;
	}
	private void updateOneResourceRole(Integer rId,String roles ){
		systemResourceDao.getJdbcDao().update("delete from system_role_acl where resource_id=?", rId);
		if (StringHelper.isEmpty(roles)) return;
		String[] s=roles.split(",");
		
		SystemRoleAcl systemRoleAcl=null;
		for (int i=0;i<s.length;i++){
			
			systemRoleAcl=new SystemRoleAcl();
			//systemRoleAcl.setResourceId(StringHelper.parseInteger(rId, 0));
			systemRoleAcl.setResourceId(rId);
			systemRoleAcl.setRoleId(StringHelper.parseInteger(s[i], 0));
			systemResourceDao.getHibernateTemplate().save(systemRoleAcl);	
		}
	}
	public void updateResourceRoles(List records) {
		// TODO Auto-generated method stub
		//查询所有的资源记录
		SystemResourceQuery query=new SystemResourceQuery();
		query.setPageSize(10000);
		query.setPageNumber(1);
		Page page=systemResourceDao.findResourcePage(query);
		List systemResourceList = (List) page.getResult();
		
		Map m=null;
		Map resourceMap=new HashMap();
		for(int i=0;systemResourceList!=null&&i<systemResourceList.size();i++){
			m=(Map)systemResourceList.get(i);			
			m.put("rootModuleName", SystemModuleCache.getRootModuleName((Integer)m.get("moduleId")));
			m.put("moduleName", SystemModuleCache.getModuleName((Integer)m.get("moduleId")));
			resourceMap.put(m.get("id"), m.get("roleIds"));
		}
		//取变化的
		Map m1=null;
		List updateList=new ArrayList();
		for(int i=0;i<records.size();i++){
			m1=(Map)records.get(i);
			//每一个资源记录，进行判断是否改变
			if (!isEqualsSet((String)m1.get("roleIds"),(String)resourceMap.get((Integer)m1.get("id")))){
				//updateList.add(m1);
				//改变的资源角色
				
				updateOneResourceRole((Integer)m1.get("id"),(String)m1.get("roleIds"));
			}
		}
		
		
		
	}
}
